package com.yida.admin;

import java.io.File;
import java.io.IOException;
import java.lang.ProcessBuilder.Redirect;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;

import com.yida.bean.phototype;
import com.yida.bean.photo;
import com.yida.service.serverphoto;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

@Controller
public class adminPhotoTypeinput {
   
	@Autowired
	public serverphoto service;
	
	@RequestMapping("/admin/phototypeinput")
	public String photo(Model mode,Integer id) {
		
		//System.out.println("kk");
		//获取原始文件名
		//String oldFileName = file.getOriginalFilename();
	    //获取文件的后缀
		//System.out.println(oldFileName);
		if (id!=null) {
			phototype phototype = service.phototypebyid(id);
			
			mode.addAttribute("phototype", phototype);
			return   "admin/phototypeinput";
		}else {
			 return  "admin/phototypeinput";
		}
		
		
     
	}
	
	@RequestMapping("/admin/phototypeup")
	public String photo(  
			              String name,
			              String flag,
			              @RequestPart("file") MultipartFile file,
			              String password) throws IllegalStateException, IOException, BadHanyuPinyinOutputFormatCombination {
		
		//创建文件夹对象
		     String slash = File.separator ;
		    String  userhome = System.getProperties().getProperty("user.home");
		    System.out.println(userhome);
		    String filehead = userhome+slash+"blog"+slash+"imgphoto"+slash;
		    File folderroot = new File(filehead);
		   
		    
		    if (!folderroot.exists()) {
		    	folderroot.mkdirs();
			}
		    
		    HanyuPinyinOutputFormat  format = new HanyuPinyinOutputFormat();
		    format.setToneType(HanyuPinyinToneType.WITHOUT_TONE); 
		    format.setVCharType(HanyuPinyinVCharType.WITH_V);
		    String pinyin =  PinyinHelper.toHanYuPinyinString(name, format ,"" , true);
		    String fileheadpinyin = filehead+pinyin;
		    File folder = new File(fileheadpinyin);
		
		//判断传入文件是不是空的 应该用感叹号 不然就是kongde
		if (!file.isEmpty()) {
			
			String filename = file.getOriginalFilename();
			
			

			Date date =  new Date();
			SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd_HH_mm_ss");
			String time = formatter.format(date);
			String suffix = filename.substring(filename.lastIndexOf("."));
		
			File file1 = new File("C:\\Users\\86155\\Desktop\\笔记\\blog-yida-9.10");
			
			//判断文件夹是否存在,创建文件夹 移入图片
			if (!folder.exists()) {
				folder.mkdir();
				
				//移入文件 separator表示分隔符
				file.transferTo(new File(fileheadpinyin+slash+time+suffix));
				
	//插入数据库类型数据	
				phototype phototype = new phototype();
				phototype.setName(name);
				phototype.setImgurl(slash+"blog"+slash+"imgphoto"+slash+pinyin+slash+time+suffix);
				phototype.setFlag(flag);
				phototype.setPassword(password);
			   service.insertphototype(phototype);
			   
			  
			   
	 //插入数据库单图片数据
			   photo photo= new photo();
			   photo.setImgurl(slash+"blog"+  slash +  "imgphoto"+  slash +pinyin+ slash +time+suffix);
			   photo.setType(phototype.getId());
			   service.insertphoto(photo);
			}else {
				  return "redirect:/admin/phototypeinput";
			}
			
		
			
		} else {
			
			 String imghead = "/static/img/";
			//判断文件夹是否存在,创建文件夹 移入图片
			if (!folder.exists()) {
				folder.mkdir();	
				//String dir = System.getProperty("user.dir");
				//移入文件 separator表示分隔符
			   //new File(dir+File.separator+filehead+pinyin+File.separator+time+suffix);
				
				phototype phototype = new phototype();
				phototype.setName(name);
				phototype.setImgurl(imghead+"/suo/suo.jpg");
				phototype.setFlag(flag);
				phototype.setPassword(password);
		//插入数据库数据		
			    service.insertphototype(phototype);  
			}else {
				  return  "redirect:/admin/phototypeinput";
			}
				
		}
		
		
		
		
		//获取原始文件名
		//String oldFileName = file.getOriginalFilename();
	    //获取文件的后缀
		
		
		
      return   "redirect:/admin/phototype";
	}
}
